package cn.ht.jywrwater.module.ht.controller.admin.waterdevices;

import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.WaterSurveyCountVO;
import cn.ht.jywrwater.module.ht.controller.admin.watersurvey.vo.WaterSurveyTypeListVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.ht.jywrwater.framework.common.pojo.PageResult;
import cn.ht.jywrwater.framework.common.pojo.CommonResult;
import static cn.ht.jywrwater.framework.common.pojo.CommonResult.success;

import cn.ht.jywrwater.framework.excel.core.util.ExcelUtils;

import cn.ht.jywrwater.framework.operatelog.core.annotations.OperateLog;
import static cn.ht.jywrwater.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.ht.jywrwater.module.ht.controller.admin.waterdevices.vo.*;
import cn.ht.jywrwater.module.ht.dal.dataobject.waterdevices.WaterDevicesDO;
import cn.ht.jywrwater.module.ht.convert.waterdevices.WaterDevicesConvert;
import cn.ht.jywrwater.module.ht.service.waterdevices.WaterDevicesService;

@Tag(name = "管理后台 - 设备")
@RestController
@RequestMapping("/ht/water-devices")
@Validated
public class WaterDevicesController {

    @Resource
    private WaterDevicesService waterDevicesService;

    @PostMapping("/create")
    @Operation(summary = "创建设备")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:create')")
    public CommonResult<Long> createWaterDevices(@Valid @RequestBody WaterDevicesCreateReqVO createReqVO) {
        return success(waterDevicesService.createWaterDevices(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:update')")
    public CommonResult<Boolean> updateWaterDevices(@Valid @RequestBody WaterDevicesUpdateReqVO updateReqVO) {
        waterDevicesService.updateWaterDevices(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ht:water-devices:delete')")
    public CommonResult<Boolean> deleteWaterDevices(@RequestParam("id") Long id) {
        waterDevicesService.deleteWaterDevices(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:query')")
    public CommonResult<WaterDevicesRespVO> getWaterDevices(@RequestParam("id") Long id) {
        WaterDevicesDO waterDevices = waterDevicesService.getWaterDevices(id);
        return success(WaterDevicesConvert.INSTANCE.convert(waterDevices));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:query')")
    public CommonResult<List<WaterDevicesRespVO>> getWaterDevicesList(@RequestParam("ids") Collection<Long> ids) {
        List<WaterDevicesDO> list = waterDevicesService.getWaterDevicesList(ids);
        return success(WaterDevicesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备分页")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:query')")
    public CommonResult<PageResult<WaterDevicesRespVO>> getWaterDevicesPage(@Valid WaterDevicesPageReqVO pageVO) {
        PageResult<WaterDevicesDO> pageResult = waterDevicesService.getWaterDevicesPage(pageVO);
        return success(WaterDevicesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备 Excel")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:export')")
    @OperateLog(type = EXPORT)
    public void exportWaterDevicesExcel(@Valid WaterDevicesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<WaterDevicesDO> list = waterDevicesService.getWaterDevicesList(exportReqVO);
        // 导出 Excel
        List<WaterDevicesExcelVO> datas = WaterDevicesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备.xls", "数据", WaterDevicesExcelVO.class, datas);
    }

    @GetMapping("/typeList")
    @Operation(summary = "获得设备种类列表")
    @Parameter(name = "types", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:query')")
    public CommonResult<List<WaterDeviceTypeListVO>> getWaterDeviceTypesList(@RequestParam("types") Collection<Integer> types) {
        List<WaterDeviceTypeListVO> list = waterDevicesService.getWaterDeviceTypesList(types);
        return success(list);
    }

    @GetMapping("/typeStatusCounts")
    @Operation(summary = "获得类型设备是否在线数量")
    @Parameter(name = "type", description = "类型", required = true, example = "1024")
    @Parameter(name = "status", description = "状态", required = true, example = "0")
    @PreAuthorize("@ss.hasPermission('ht:water-devices:query')")
    public CommonResult<WaterDeviceCountVO> typeStatuscounts(@RequestParam("type") Integer type, @RequestParam("status") Integer status) {
        WaterDeviceCountVO result = waterDevicesService.countByTypeAndStatus(type,status);
        return success(result);
    }


}
